package org.example.myframework.frame.common.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回结果
 */
@Data
public class R<T> implements Serializable {

    // 定义序列化值
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("是否成功")
    private boolean success;

    @ApiModelProperty("消息状态码")
    private int code;

    @ApiModelProperty("错误状态码")
    private int errorCode;

    @ApiModelProperty("消息内容")
    private String msg;

    @ApiModelProperty("数据对象")
    private T data;

    // 成功响应参数200
    public static final int SUCCESS = 200;

    public static final String SUCCESS_MSG = "操作成功";
    // 失败响应参数500
    public static final int FAIL = 500;

    public static final String FAIL_MSG = "操作失败";

    /**************************************统一成功返回*********************************************/
    /**
     * 成功结果返回（有参，无参）
     */
    public static <E> R<E> ok() {
        return new R();
    }
    public static <E> R<E> ok(E e) {
        return new R(e);
    }

    /**************************************统一失败返回*********************************************/
    /**
     * 失败结果返回（有参，无参）
     */
    public static <E> R<E> fail() {
        return new R(false, FAIL, FAIL_MSG);
    }
    public static <E> R<E> fail(E e) {
        return new R(false, FAIL, FAIL_MSG, e);
    }
    public static <E> R<E> error(Integer errorCode, String msg, E data) {
        return new R(false, FAIL, errorCode, msg, data);
    }

    /**
     * 成功结构构造（有参，无参）
     */
    public R() {
        this(true, SUCCESS, SUCCESS_MSG, null);
    }
    public R(T data) {
        this(true, SUCCESS, SUCCESS_MSG, data);
    }

    /**
     * 无参数，全参数结果构造
     */
    public R(Boolean success, Integer code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     *
     * @param success
     * @param code
     * @param msg
     * @param data
     */
    public R(Boolean success, Integer code, Integer errorCode, String msg, T data) {
        this.success = success;
        this.code = code;
        this.errorCode = errorCode;
        this.msg = msg;
        this.data = data;
    }
    public R(Boolean success, Integer code, String msg) {
        this(success, code, msg, null);
    }



}
